﻿using ServiceCore.DbContext;
using ServiceCore.Entity.Core;
using ServiceCore.Entity.Model;
using ServiceCore.Entity.Param;
using ServiceCore.Entity.Param.Map;

namespace ServiceCore.Service
{
    /// <summary>
    /// 映射服务
    /// </summary>
    public partial class MapService : SqlsugarServices<MapInfo>
    {
        public BaseParam<List<MapInfo>, long> Map_SaveList_Verify(BaseParam<List<MapInfo>, long> arg)
        {
            if (arg == null)
                return new BaseParam<List<MapInfo>, long>("参数不能为空");
            if (arg.Param == null)
                return new BaseParam<List<MapInfo>, long>("参数不能为空");
            if (arg.Param.Any() == false)
                return new BaseParam<List<MapInfo>, long>("参数不能为空");
            if (arg.Param.Where(x => string.IsNullOrEmpty(x.MapType)).Any())
                return new BaseParam<List<MapInfo>, long>("管理类别不能为空");
            if (arg.Param.Where(x => x.DataId == 0).Any())
                return new BaseParam<List<MapInfo>, long>("关联Id（主）不能为空");
            if (arg.Param.Where(x => string.IsNullOrEmpty(x.BindId)).Any())
                return new BaseParam<List<MapInfo>, long>("关联Id（从）不能为空");

            var idx = 1;
            arg.Param.ForEach(x => { x.SortBy = idx++; });
            
            return arg;
        }

        public BaseParam<IdsParam, long> Map_DeleteList_Verify(BaseParam<IdsParam, long> arg)
        {
            if (arg == null)
                return new BaseParam<IdsParam, long>("参数不能为空");
            if (arg.Param == null)
                return new BaseParam<IdsParam, long>("参数不能为空");
            if (arg.Param.Ids == null)
                return new BaseParam<IdsParam, long>("参数不能为空");
            if (arg.Param.Ids.Any() == false)
                return new BaseParam<IdsParam, long>("参数不能为空");

            return arg;
        }

        /// <summary>
        /// 分页 
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<MapTagParam, MapTagParam> Map_TagType_List_Verify(BaseParam<MapTagParam, MapTagParam> arg)
        {
            if (arg == null)
                return new BaseParam<MapTagParam, MapTagParam>("参数不能为空");
            if (arg.Param == null)
                return new BaseParam<MapTagParam, MapTagParam>("参数不能为空");
            if (string.IsNullOrEmpty(arg.Param.MapType))
                return new BaseParam<MapTagParam, MapTagParam>("绑定类型不能为空");
            if (string.IsNullOrEmpty(arg.Param.BindId))
                return new BaseParam<MapTagParam, MapTagParam>("绑定Id不能为空");

            return arg;
        }

        public BaseParam<MapTagParam, int> Map_TagType_Save_Verify(BaseParam<MapTagParam, int> arg)
        {
            if (arg == null)
                return new BaseParam<MapTagParam, int>("参数不能为空");
            if (arg.Param == null)
                return new BaseParam<MapTagParam, int>("参数不能为空");           
            if (string.IsNullOrEmpty(arg.Param.MapType))
                return new BaseParam<MapTagParam, int>("绑定类型不能为空");
            if (string.IsNullOrEmpty(arg.Param.BindId))
                return new BaseParam<MapTagParam, int>("绑定Id不能为空");
            if (arg.Param.lstDataId == null)
                return new BaseParam<MapTagParam, int>("数据不能为空");
            if (arg.Param.lstDataId.Any() == false)
                return new BaseParam<MapTagParam, int>("数据不能为空");

            var i = 1;
            arg.Param.lstDataId.ForEach(x =>
            {
                arg.Param.lstBind.Add(new MapInfo()
                {
                    MapType = arg.Param.MapType,
                    BindId = arg.Param.BindId,
                    DataId = long.Parse(x),
                    SortBy = i++
                });
            });

            return arg;
        }
        
    }
}
